<?php
Yii::import('application.vendors.*');
require_once('zencoder/Zencoder.php');

class ZencoderVideoEncoder extends CApplicationComponent
{
	const ZENCODER_API_KEY = 'e099a4080a4cb6f47efb36b296f27fc7';//old free one -'3818934eba38415879890cddf04d8a9e';
	const OUTPUT_FORMAT = '.m4v';
	
	public function sendRequest($inputFile)
	{	
		try {
			  // Initialize the Services_Zencoder class
			  $zencoder = new Services_Zencoder(self::ZENCODER_API_KEY);
			
			  // New Encoding Job
			  $encoding_job = $zencoder->jobs->create('
			    {
			      "input": "http://aussieapples.neonfields.com/media/'.$inputFile.'",
			      "outputs": [
                                        {
			        	"thumbnails": {
			        		"format": "jpg",
					        "size": "480x320",
					        "base_url": "s3://files.neonfields.com",
					        "filename": "'.Content::getFileName($inputFile).'",
					        "public": 1
					     },
						"format": "m4v",
						"video_codec": "h264",
						"quality": 2,
						"size": "480x320",
						"url": "s3://files.neonfields.com/'.Content::getFileName($inputFile).self::OUTPUT_FORMAT.'",
						"public": 1,
                                                "notifications":[
                                		      {"format": "json", "url": "http://wizard.neonfields.com/video/default/notify"}
                                        	]
					},
					{			        
						"format": "webm",
      					"video_codec": "vp8",
      					"audio_codec": "vorbis",
						"quality": 2,
						"size": "480x320",
						"url": "s3://files.neonfields.com/'.Content::getFileName($inputFile).'.webm'.'",
						"public": 1						
					}
			      ]
			    }
			  ');
			  
			  //"notifications":[
			//		      {"format": "json", "url": "http://wizard.neonfields.com/video/default/notify"}
			//		     ]
			
			  // Success if we got here
			  //echo "Job ID: ".$encoding_job->id."\n";
		} catch (Services_Zencoder_Exception $e) {
			  // If were here, an error occured
			  //echo "Fail :(\n\n";
			  //echo "Errors:\n";
			  //foreach ($e->getErrors() as $error) echo $error."\n";
			  //echo "Full exception dump:\n\n";
			  print_r($e);
		}																											
	}
	
	public function handleNotification()
	{
		// Initialize the Services_Zencoder class
		$zencoder = new Services_Zencoder(self::ZENCODER_API_KEY);
		
				
		// Catch notification
		$notification = $zencoder->notifications->parseIncoming();
		
		echo Yii::trace(CVarDumper::dumpAsString($notification),'vardump');
		
		// Check output/job state
		if($notification->output->state == "finished") 
		{
		  echo "success";	
		  return $notification;
		  // If you're encoding to multiple outputs and only care when all of the outputs are finished
		  // you can check if the entire job is finished.
		  //if($notification->job->state == "finished") 
		  //   echo "success";
		  
		}elseif ($notification->output->state == "cancelled") 
		{
		  	echo "Cancelled!\n";
		}else
		{
			echo "Fail!\n";
			echo $notification->output->error_message."\n";
			echo $notification->output->error_link;
		}
		return null;
	}
}
?>